package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.service.AgreementService;
import com.cjack.frenchexcel.table.AgreementCompanyTable;
import com.cjack.frenchexcel.table.AgreementTrialTable;
import com.cjack.frenchexcel.table.AgreementVisitorTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 用户的合同信息管理
 * Created by root on 4/21/19.
 */
@RestController
@RequestMapping("/agreement")
public class AgreementEndpoint extends BaseEndpoint{

    @Autowired
    AgreementService service;

    /**
     * 员工资料- 试用/CDD合同
     * @return
     */
    @RequestMapping(value = "/saveTrial", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveTrial( AgreementTrialTable table) {
        if( table.getId() == null){
            table.setCreateTime( new Date());
        }
        table.setUpdateTime( new Date());
        Long id = service.saveTrial( table);
        return AjaxResult.SUCCESS( id);
    }

    /**
     * 员工资料- 公司合同
     * @return
     */
    @RequestMapping(value = "/saveCompany", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveCompany( AgreementCompanyTable table) {
        if( table.getId() == null){
            table.setCreateTime( new Date());
        }
        table.setUpdateTime( new Date());
        Long id = service.saveCompany( table);
        return AjaxResult.SUCCESS( id);
    }

    /**
     * 员工资料- 游客合同
     * @return
     */
    @RequestMapping(value = "/saveVisitor", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveVisitor( AgreementVisitorTable table) {
        if( table.getId() == null){
            table.setCreateTime( new Date());
        }
        table.setUpdateTime( new Date());
        Long id = service.saveVisitor( table);
        return AjaxResult.SUCCESS( id);
    }

    /**
     * 获取合同
     * @return
     */
    @RequestMapping(value = "/getAgreement", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getAgreement( @RequestParam("agreementType")String agreementType, @RequestParam("agreementId")Long agreementId) {

        switch ( agreementType){
            case "1":
                return AjaxResult.SUCCESS( service.getTrial( agreementId));
            case "2":
                return AjaxResult.SUCCESS( service.getTrial( agreementId));
            case "3":
                return AjaxResult.SUCCESS( service.getCompany( agreementId));
            case "4":
                return AjaxResult.SUCCESS( service.getVisitor( agreementId));
        }
        return AjaxResult.ERROR();
    }
}
